/*******************************************************************************
 * Copyright (c) 2011 Jovansonlee B. Cesar.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Jovansonlee B. Cesar - initial API and implementation
 ******************************************************************************/
package com.ivanceras.server.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.ivanceras.shared.db.IDatabase;
import com.ivanceras.shared.db.SchemaTable;
import com.ivanceras.shared.exception.DatabaseException;

public class DB_SQLDroid extends DB_Jdbc implements IDatabase {

	private final String DRIVER_CLASS = "com.lemadi.storage.database.sqldroid.SqldroidDriver";
	
	public DB_SQLDroid(String dataFile) {
		this.dbName = dataFile;
		init();
	}
	/**
	 * non-static to allow multiple database connection
	 */
	public boolean init(){
		if(initialized) return true;
		try {
			if(isDriverAvailable(DRIVER_CLASS)){
				connection = getDBConnection(getDBUrl());
				if(connection == null){
					System.err.println("Unable to connect to Database");
					return false;
				}else{
					initialized = true;
					return true;
				}
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	protected Connection getDBConnection(String dbUrl)
	{
		Connection c = null;		  
		try {
			c = DriverManager.getConnection(dbUrl);			
		} catch (SQLException se) 
		{
			System.err.println("Unable to connect to database "+dbUrl);
			se.printStackTrace();
		}
		return c;
	}
	
	private String getDBUrl(){
		return "jdbc:sqldroid://"+dbName;
	}
	
	@Override
	protected String getAutoIncrementColumnConstraint() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected String getRealTableName(String schema, String tableName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Columns getColumnDetails(String schema,
			String tableName) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public SchemaTable[] getTableNames(String schema, String tablePattern)
			throws DatabaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
